package com.ohxwj.modules.sys.controller;

import com.ohxwj.common.utils.Constant;
import com.ohxwj.common.utils.R;
import com.ohxwj.modules.sys.entity.SysSchoolEntity;
import com.ohxwj.modules.sys.service.SysSchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 学校管理
 * 
 * @author xuwenjie
 * @date 2017-10-17 15:23:47
 */
@RestController
@RequestMapping("/sys/school")
public class SysSchoolController extends AbstractController {
	@Autowired
	private SysSchoolService sysSchooltService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:school:list")
	public List<SysSchoolEntity> list(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只能查询本部门及子部门数据
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("schoolFilter", sysSchooltService.getSubSchoolIdList(getSchoolId()));
		}
		List<SysSchoolEntity> schoolList = sysSchooltService.queryList(map);

		return schoolList;
	}

	/**
	 * 选择学校(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:school:select")
	public R select(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只能查询本学校及子校区数据
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("schoolFilter", sysSchooltService.getSubSchoolIdList(getSchoolId()));
		}
		List<SysSchoolEntity> schoolList = sysSchooltService.queryList(map);

		//添加一级学校
		if(getUserId() == Constant.SUPER_ADMIN){
			SysSchoolEntity root = new SysSchoolEntity();
			root.setSchoolId(0L);
			root.setName("一级学校");
			root.setParentId(-1L);
			root.setOpen(true);
			schoolList.add(root);
		}

		return R.ok().put("schoolList", schoolList);
	}

	/**
	 * 上级学校Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:school:list")
	public R info(){
		long schoolId = 0;
		if(getUserId() != Constant.SUPER_ADMIN){
			SysSchoolEntity school = sysSchooltService.queryObject(getSchoolId());
			schoolId = school.getParentId();
		}

		return R.ok().put("schoolId", schoolId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{schoolId}")
	@RequiresPermissions("sys:school:info")
	public R info(@PathVariable("schoolId") Long schoolId){
		SysSchoolEntity school = sysSchooltService.queryObject(schoolId);
		
		return R.ok().put("school", school);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:school:save")
	public R save(@RequestBody SysSchoolEntity school){
		sysSchooltService.save(school);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:school:update")
	public R update(@RequestBody SysSchoolEntity school){
		sysSchooltService.update(school);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:school:delete")
	public R delete(long schoolId){
		//判断是否有子校区
		List<Long> schoolList = sysSchooltService.querySchoolIdList(schoolId);
		if(schoolList.size() > 0){
			return R.error("请先删除子校区");
		}

		sysSchooltService.delete(schoolId);
		
		return R.ok();
	}
	
}
